<?php
/**
 * Created By base
 * Author: Virus
 * Date: 2020/3/31
 * Time: 17:03
 */

namespace app\admin\logic;


use app\common\model\mysql\Ip;

class IpLogic
{
    /**
     * 模型对象
     * @var Ip|null
     */
    protected $model = null;

    public function __construct()
    {
        $this->model = new Ip();
    }

    /**
     * 显示ip地址列表
     * @return mixed
     */
    public function getLists()
    {
        $result = $this->model->getList();

        return $result->toArray();
    }

    /**
     * 添加ip地址
     * @param $data
     * @return bool
     * @throws \think\Exception
     */
    public function addIp($data)
    {
        $result = $this->model->addData($data);
        if (!$result) {
            throw new \think\Exception("ip地址添加失败");
        }

        return true;
    }

    /**
     * 获取单个ip地址详细信息
     * @param $id
     * @return array|mixed
     */
    public function getIpOneById($id)
    {
        $result = $this->model->getOneDataById($id);
        if (empty($result)) {
            return [];
        }

        return $result;
    }

    /**
     * 更新ip地址
     * @param $id
     * @param $data
     * @return bool
     * @throws \think\Exception
     */
    public function updateIp($id, $data)
    {
        $result = $this->model->updateDataById($id, $data);
        if (false === $result) {
            throw new \think\Exception("ip地址编辑失败");
        }

        return true;
    }

    /**
     * 删除ip地址
     * @param $id
     * @return mixed
     * @throws \think\Exception
     */
    public function deleteById($id)
    {
        $result = $this->model->deleteOneData($id);
        if (!$result) {
            throw new \think\Exception("ip删除失败");
        }

        return $result;
    }
}